
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using AtDevice;

namespace Interoperability
{
    
    
    public class PhonebookXmlImport : IPhonebookImport
    {
        private void ImportDump(IPhoneBook phonebook, string filename, IEnumerable<PhoneBookEntry> entries)
        {
            foreach(var entry in entries)
            {
                phonebook.UpdateEntry(entry);
            }
        }
        
        public void ImportPhonebook(IPhoneBook phonebook, string filename, PhonebookImportStyle style)
        {
            XDocument document = XDocument.Load(filename);
            var contacts = document.Elements("contacts");

            IEnumerable<PhoneBookEntry> entries = from element in contacts.Elements("contact")
                           select new PhoneBookEntry() {
                                Index = (int)element.Attribute("id"),
                                Name = (string)element.Element("name"),
                                MobileNumber = (string)element.Element("number")
                            };

            switch(style)
            {
            case PhonebookImportStyle.Dump:
                ImportDump(phonebook, filename, entries);
                break;
            default:
                throw new InvalidOperationException();
            }
        }
    }
}
